package org.iqiyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.iqiyi.entity.Auth;
import org.iqiyi.entity.JWT;
import org.iqiyi.entity.UserEntity;
import org.iqiyi.feign.AuthServiceFeign;
import org.iqiyi.mapper.UserMapper;
import org.iqiyi.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author huqi
 * @since 2021-04-17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {
    @Resource
    private AuthServiceFeign authServiceFeign;
    //base64加密
    private static final String ServerAuthorization = "Basic dXNlcjoxMjM0NTY=";

    @Override
    public Auth login(String userName, String password) {
        JWT jwt = authServiceFeign.getToken(ServerAuthorization,
                "password", userName, password);
        UserEntity user = this.getOne(new QueryWrapper<UserEntity>().eq(UserEntity.USERNAME, userName).eq(UserEntity.DATA_ACTIVE, 0));
        Auth auth = new Auth();
        auth.setId(user.getId());
        auth.setHandeUrl(user.getHandeUrl());
        auth.setUsername(user.getUsername());
        auth.setAccessToken(jwt.getAccess_token());
        auth.setJti(jwt.getJti());
        auth.setTokenType(jwt.getToken_type());
        auth.setRefreshToken(jwt.getRefresh_token());
        auth.setExpiresIn(jwt.getExpires_in());
        return auth;
    }
}
